/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import AccesoADatos.ServicioPermisoTableSpace;
import Logica.Clasificacion;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author David
 */
public class EditarClasificacion extends javax.swing.JFrame {

    /**
     * Creates new form NuevaClasificacion
     */
    Catalogo catalogo;
    int caso;
    int index;
    private ServicioPermisoTableSpace serTS;
    
    public EditarClasificacion(Catalogo catalogo,String user,String pass,String ip,String port,String bd) {
        initComponents();
        caso = 1;
        serTS=new ServicioPermisoTableSpace(user,pass,ip,port,bd);
        this.catalogo = catalogo;
    }
    public EditarClasificacion(Catalogo catalogo, int index) {
        initComponents();
        caso = 2;
        this.index = index;
        this.catalogo = catalogo;
        Clasificacion c = catalogo.clasificaciones.get(index);
        clasifTF.setText(c.getClasificacion());
        descTA.setText(c.getDescripcion());
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        clasifTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descTA = new javax.swing.JTextArea();
        aceptBoton = new javax.swing.JButton();
        cancelBoton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Clasification:");

        clasifTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clasifTFActionPerformed(evt);
            }
        });

        jLabel2.setText("Description:");

        descTA.setColumns(20);
        descTA.setRows(5);
        jScrollPane1.setViewportView(descTA);

        aceptBoton.setText("Accept");
        aceptBoton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptBotonActionPerformed(evt);
            }
        });

        cancelBoton.setText("Cancel");
        cancelBoton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBotonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(jLabel1))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(clasifTF, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(aceptBoton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBoton)))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clasifTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptBoton)
                    .addComponent(cancelBoton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clasifTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clasifTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_clasifTFActionPerformed

    private void aceptBotonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptBotonActionPerformed
       
        boolean exito = true;
        if(clasifTF.getText().equals("")||descTA.getText().equals("")){
            exito=false;
            
            JOptionPane.showMessageDialog(this, "Empty spaces", "Warning", 1, null);
        }
        
        if(exito && caso==1){
            
            try {
                System.out.println(clasifTF.getText());
                serTS.createRole(clasifTF.getText());
                serTS.grantPrivilege("connect", clasifTF.getText());
                serTS.grantPrivilege("resource", clasifTF.getText());
                
                
                catalogo.clasificaciones.add(new Clasificacion(clasifTF.getText(),descTA.getText()));
                ((DefaultTableModel) catalogo.getClassTabla().getModel()).addRow(new Object[]{clasifTF.getText(),descTA.getText()});
                catalogo.guardarArchivo();
            
            
            } catch (    SQLException | ClassNotFoundException ex) {
                Logger.getLogger(EditarClasificacion.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.dispose();
        }
        if(exito && caso==2){
            catalogo.clasificaciones.get(index).setClasificacion(clasifTF.getText());
            catalogo.clasificaciones.get(index).setDescripcion(descTA.getText());
            catalogo.getClassTabla().setValueAt(clasifTF.getText(), index, 0);
            catalogo.getClassTabla().setValueAt(descTA.getText(), index, 1);
            
            catalogo.guardarArchivo();
            this.dispose();
        }
    }//GEN-LAST:event_aceptBotonActionPerformed

    private void cancelBotonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBotonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_cancelBotonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptBoton;
    private javax.swing.JButton cancelBoton;
    private javax.swing.JTextField clasifTF;
    private javax.swing.JTextArea descTA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
